#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regression tests for toktx

# ---------------------------------------------------------------
#
# Copyright (c) 2010 The Khronos Group Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and/or associated documentation files (the
# "Materials"), to deal in the Materials without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Materials, and to
# permit persons to whom the Materials are furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Materials.
# 
# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
# 
# ---------------------------------------------------------------

# Make paths relative to location of this script.
mydir=${0%/*}/..
toktx_vs2013=$mydir/build/msvs/win/vs2013/x64/Release/toktx.exe
toktx_vs2015=$mydir/build/msvs/win/vs2015/x64/Release/toktx.exe
toktx_cmake=$mydir/build/cmake/toktx/toktx
toktx_gmake=$mydir/build/GNUmake/toktx/toktx
testimages=$mydir/testimages

declare -i numtests=0
declare -i passed=0
declare -i failed=0

# Ensure test is not polluted by user environment
unset TOKTX_OPTIONS

if [ -n "$1" -a -x "$1" ]; then
  toktx="$1"
elif [ -x "$toktx_vs2013" ]; then
  toktx=$toktx_vs2013
elif [ -x "$toktx_vs2015" ]; then
  toktx=$toktx_vs2015
elif [ -x "$toktx_gmake" ]; then
  toktx=$toktx_cmake
elif [ -x "$toktx_cmake" ]; then
  toktx=$toktx_cmake
else
  echo $0: None of $toktx_vs2013, $toktx_vs2015, $toktx_gmake or $toktx_cmake found.
  echo $0: Aborting test
  exit 1
fi

numtests=$numtests+1
if $toktx --help 2> /dev/null; then
  passed=$passed+1
else
  echo "--help not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
if $toktx --version 2> /dev/null; then
  passed=$passed+1
else
  echo "--version not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --foobar 2> /dev/null; then
  passed=$passed+1
else
  echo "invalid option --foobar accepted"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --automipmap --mipmaps a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --automipmap & --mipmaps allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --alpha --luminance a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --alpha & --luminance allowed"
  failed=$failed+1
fi

function cmpktx () {
  if diff $1 $2; then
    passed=$passed+1
  else
	failed=$failed+1
	echo "created ktx file differs from target $2"
  fi
}

# Generate ktx file and compare with reference
# gencmpktx <reference> <toktx args> <toktx infile> ...
function gencmpktx() {
  numtests=$numtests+1
  local args
  local reference=$1; shift
  for i in $*; do
	if [ ${i:0:2} == "--" ]; then
	  args="$args $i"
	  shift
	fi
  done
  #echo $toktx $args $tempfile $*
  $toktx $args $tempfile $*
  cmpktx $tempfile $reference
  rm $tempfile
}

cd $testimages
# Git Bash does not include mktemp.
if which mktemp > /dev/null; then
  tempfile=$(mktemp toktxXXXX.ktx)
else
  tempfile=/tmp/toktxXXXX.ktx
fi

gencmpktx rgb-reference.ktx --lower_left_maps_to_s0t0 rgb.ppm 
gencmpktx rgb-amg-reference.ktx --automipmap --lower_left_maps_to_s0t0 rgb.ppm
TOKTX_OPTIONS=--lower_left_maps_to_s0t0 gencmpktx up-reference.ktx up.ppm
gencmpktx down-reference.ktx up.ppm
gencmpktx rgb-mipmap-reference.ktx --lower_left_maps_to_s0t0 --mipmap level0.ppm level1.ppm level2.ppm level3.ppm level4.ppm level5.ppm level6.ppm
gencmpktx rgba-reference.ktx --lower_left_maps_to_s0t0 rgba.pam
gencmpktx luminance_unsized_reference.ktx --luminance luminance.pgm
gencmpktx luminance_sized_reference.ktx --luminance --sized luminance.pgm

numtests=$numtests+1
TOKTX_OPTIONS=--lower_left_maps_to_s0t0 $toktx - rgb.ppm > $tempfile
cmpktx $tempfile rgb-reference.ktx
rm $tempfile

numtests=$numtests+1
cat rgb.ppm | $toktx --lower_left_maps_to_s0t0 - > $tempfile
cmpktx $tempfile rgb-reference.ktx
rm $tempfile

numtests=$numtests+1
cat rgb.ppm | $toktx --lower_left_maps_to_s0t0 $tempfile
cmpktx $tempfile rgb-reference.ktx
rm $tempfile

echo "Tests run: $numtests; passed: $passed; failed: $failed"
if [ $failed -gt 0 ]; then
  exit 1;
else
  exit 0;
fi

